﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 逻辑
{
    public class 炮 : QiZi
    {
        public 炮(int x, int y, QiPan q, Button[,] b, int p)
            : base("炮", x, y, q, p, b)
        {
        }

        public override void CanPut()
        {
            canPut.Clear();
            PaoXia();
            PaoShang();
            PaoYou();
            PaoZuo();
        }

        private void PaoShang()
        {
            int x1 = x;
            int k = 0;
            while (true)
            {
                if (k == 0)
                {
                    x1--;
                    if (x1 < 0)
                        break;
                    if (buttons[x1, y].Tag == null)
                    {
                        int z = Game.ChangeDirection(this.Player,x1);
                        canPut.Add(z * 10 + y);
                    }
                    else
                    {
                        k = 1;
                    }
                }
                else if (k == 1)
                {
                    x1--;
                    if (x1 < 0)
                        break;
                    if (buttons[x1, y].Tag == null)
                    {
                        continue;
                    }
                    else if ((buttons[x1, y].Tag as QiZi).Player != this.Player)
                    {
                        int z = Game.ChangeDirection(this.Player,x1);
                        canPut.Add(z * 10 + y);
                        break;
                    }
                    else if ((buttons[x1, y].Tag as QiZi).Player == this.Player)
                    {
                        break;
                    }
                }

                
            }
        }

        private void PaoYou()
        {
            int x1 = y;
            int k = 0;
            while (true)
            {
                if (k == 0)
                {
                    x1++;
                    if (x1 > 8)
                        break;
                    if (buttons[x, x1].Tag == null)
                    {
                        int z = Game.ChangeDirection(this.Player, x);
                        canPut.Add(z * 10 + x1);
                    }
                    else
                    {
                        k = 1;
                    }
                }
                else if (k == 1)
                {
                    x1++;
                    if (x1 > 8)
                        break;
                    if (buttons[x, x1].Tag == null)
                    {
                        continue;
                    }
                    else if ((buttons[x, x1].Tag as QiZi).Player != this.Player)
                    {
                        int z = Game.ChangeDirection(this.Player, x);
                        canPut.Add(z * 10 + x1);
                        break;
                    }
                    else if ((buttons[x, x1].Tag as QiZi).Player == this.Player)
                    {
                        break;
                    }
                }
            }
        }

        private void PaoZuo()
        {
            int x1 = y;
            int k = 0;
            while (true)
            {
                if (k == 0)
                {
                    x1--;
                    if (x1 < 0)
                        break;
                    if (buttons[x, x1].Tag == null)
                    {
                        int z = Game.ChangeDirection(this.Player, x);
                        canPut.Add(z * 10 + x1);
                    }
                    else
                    {
                        k = 1;
                    }
                }
                else if (k == 1)
                {
                    x1--;
                    if (x1 < 0)
                        break;
                    if (buttons[x, x1].Tag == null)
                    {
                        continue;
                    }
                    else if ((buttons[x, x1].Tag as QiZi).Player != this.Player)
                    {
                        int z = Game.ChangeDirection(this.Player, x);
                        canPut.Add(z * 10 + x1);
                        break;
                    }
                    else if ((buttons[x, x1].Tag as QiZi).Player == this.Player)
                    {
                        break;
                    }
                }
            }
        }

        private void PaoXia()
        {
            int x1 = x;
            int k = 0;
            while (true)
            {
                if (k == 0)
                {
                    x1++;
                    if (x1 > 9)
                        break;
                    if (buttons[x1, y].Tag == null)
                    {
                        int z = Game.ChangeDirection(this.Player, x1);
                        canPut.Add(z * 10 + y);
                    }
                    else
                    {
                        k = 1;
                    }
                }
                else if (k == 1)
                {
                    x1++;
                    if (x1 > 9)
                        break;
                    if (buttons[x1, y].Tag == null)
                    {
                        continue;
                    }
                    else if ((buttons[x1, y].Tag as QiZi).Player != this.Player)
                    {
                        int z = Game.ChangeDirection(this.Player, x1);
                        canPut.Add(z * 10 + y);
                        break;
                    }
                    else if ((buttons[x1, y].Tag as QiZi).Player == this.Player)
                    {
                        break;
                    }
                }
            }
        }
    }
}
